<template>
  <div center>
    <el-form ref="info2" :inline="true" :rules="rules" :model="info" size="mini" label-position="right"
             label-width="150px">
      <el-form-item label="受检单位" prop="sjcdwmc">
        <el-input v-model="info.sjcdwmc" placeholder="请输入内容" :disabled="true"/>
      </el-form-item>
      <el-form-item label="项目编号" prop="xmbh">
        <el-input v-model="info.xmbh" placeholder="请输入内容" :disabled="true"/>
      </el-form-item>
      <el-form-item label="收样日期" prop="syrq">
        <el-date-picker
          v-model="info.syrq"
          :disabled="isShow"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="检测日期-开始" prop="jcrq">
        <el-date-picker
          v-model="info.jcrq"
          :disabled="isShow"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="检测日期-结束" prop="jcrqend">
        <el-date-picker
          v-model="info.jcrqend"
          :disabled="isShow"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-divider></el-divider>
      <el-form-item v-if="!isShow">
        <el-button type="warning" size="mini" @click="addTable1(null,null)">添加</el-button>
      </el-form-item>
      <el-table key="table1" border :data="info.table1Json" style="width: 100%">
        <el-table-column
          prop="sampleNumber1"
          align="center"
          label="样品编号">
        </el-table-column>
        <el-table-column
          align="center"
          label="采样前称重">
          <el-table-column
            prop="samplingBeforeValue1"
            align="center"
            label="干燥时间">
          </el-table-column>
          <el-table-column
            prop="samplingBeforeValue2"
            align="center"
            label="m1（mg）">
          </el-table-column>
          <el-table-column
            prop="samplingBeforeValue3"
            align="center"
            label="m2（mg）">
          </el-table-column>
        </el-table-column>
        <el-table-column
          align="center"
          label="采样后称重">
          <el-table-column
            prop="samplingAfterValue1"
            align="center"
            label="干燥时间">
          </el-table-column>
          <el-table-column
            prop="samplingAfterValue2"
            align="center"
            label="m3（mg）">
          </el-table-column>
          <el-table-column
            prop="samplingAfterValue3"
            align="center"
            label="m4（mg）">
          </el-table-column>
        </el-table-column>
        <el-table-column
          prop="value1"
          align="center"
          label="m4-m2（mg）">
        </el-table-column>
        <el-table-column
          prop="value2"
          align="center"
          label="V(L/min)">
        </el-table-column>
        <el-table-column
          prop="value3"
          align="center"
          label="t(min)">
        </el-table-column>
        <el-table-column
          prop="value4"
          align="center"
          label="C(mg/m³)">
        </el-table-column>
        <el-table-column
          align="center"
          fixed="right"
          label="操作" v-if="!isShow">
          <template slot-scope="scope">
            <el-button @click="addTable1(scope.$index,scope.row)" type="text" size="small">编辑</el-button>
            <el-button type="text" size="small" @click="delTable1(scope.$index,scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-divider></el-divider>
      <el-form-item label="检验员" prop="jyy">
        <el-select v-model="info.jyy" :disabled="isShow" multiple placeholder="请选择" :multiple-limit="2">
          <el-option
            v-for="item in userList"
            :label="item.nickName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="复核员" prop="fhy">
        <el-select v-model="info.fhy" :disabled="isShow" multiple placeholder="请选择" :multiple-limit="2">
          <el-option
            v-for="item in userList"
            :label="item.nickName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <el-divider></el-divider>
    <div style="text-align: center" v-if="!isShow">
      <el-button type="primary" size="small" @click="addOrEdit">保存</el-button>
    </div>
    <el-dialog :visible.sync="centerDialogVisible" append-to-body center width="80%">
      <el-form ref="tableInfo" :inline="true" :rules="rules" :model="tableInfo" size="mini" label-position="right"
               label-width="120px">
        <el-form-item label="样品编号" prop="sampleNumber1">
          <el-input v-model="tableInfo.sampleNumber1" placeholder="请输入内容"/>
        </el-form-item>
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>采样前称重</span>
          </div>
          <el-form-item label="干燥时间" prop="samplingBeforeValue1">
            <el-input v-model="tableInfo.samplingBeforeValue1" placeholder="请输入内容" :disabled="isShow"/>
          </el-form-item>
          <el-form-item label="m1（mg）" prop="samplingBeforeValue2">
            <el-input v-model="tableInfo.samplingBeforeValue2" placeholder="请输入内容" :disabled="isShow"/>
          </el-form-item>
          <el-form-item label="m2（mg）" prop="samplingBeforeValue3">
            <el-input v-model="tableInfo.samplingBeforeValue3" @change="jisuanjg()" placeholder="请输入内容"
                      :disabled="isShow"/>
          </el-form-item>
        </el-card>
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>采样后称重</span>
          </div>
          <el-form-item label="干燥时间" prop="samplingAfterValue1">
            <el-input v-model="tableInfo.samplingAfterValue1" placeholder="请输入内容" :disabled="isShow"/>
          </el-form-item>
          <el-form-item label="m3（mg）" prop="samplingAfterValue2">
            <el-input v-model="tableInfo.samplingAfterValue2" placeholder="请输入内容" @change="setM4()" :disabled="isShow"/>
          </el-form-item>
          <el-form-item label="m4（mg）" prop="samplingAfterValue3">
            <el-input v-model="tableInfo.samplingAfterValue3" @change="jisuanjg()" placeholder="请输入内容"
                      :disabled="isShow"/>
          </el-form-item>
        </el-card>
        <el-form-item label="m4-m2（mg）" prop="value1">
          <el-input v-model="tableInfo.value1" placeholder="请输入内容" :disabled="isShow"/>
        </el-form-item>
        <el-form-item label="V(L/min)" prop="value2">
          <el-input v-model="tableInfo.value2" placeholder="请输入内容" @change="jisuanjg()" :disabled="isShow"/>
        </el-form-item>
        <el-form-item label="t(min)" prop="value3">
          <el-input v-model="tableInfo.value3" placeholder="请输入内容" @change="jisuanjg()" :disabled="isShow"/>
        </el-form-item>
        <el-form-item label="C(mg/m³)" prop="value4">
          <el-input v-model="tableInfo.value4" placeholder="请输入内容" :disabled="isShow"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="centerDialogVisible = false">关 闭</el-button>
        <el-button type="primary" size="mini" @click="addOrEditTable1">确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
// import imgUpdate from '@/components/Upload/imgUpdate'
// import {addOrEdit, getInfo} from '@/api/table/tableSix'
// import {getUserList} from '@/api/system/sysUser'
// import {getUserId} from '@/utils/auth'
import imgUpdate from '@/components/Upload/imgUpdate'
import {addOrEdit, getInfo} from '@/api/table/tableSix'
import {getUserList} from '@/api/system/sysUser'
import {getUserId} from '@/utils/auth'

export default {
  components: {
    imgUpdate
  },
  props: {
    noCreated: {
      type: Boolean,
      default: null
    },
    isShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      info: {},
      userId: getUserId(),
      userList: [],
      tableInfo: {},
      tableIndex: null,
      table1Id: null,
      centerDialogVisible: false,
      centerDialogVisible2: false,
      rules: {
        dzr: [{required: true, message: '请输入内容', trigger: 'chang'}],
        sjcdwptr: [{required: true, message: '请输入内容', trigger: 'chang'}],
        cyrq: [{required: true, message: '请选择日期', trigger: 'chang'}],
        dzrq: [{required: true, message: '请选择日期', trigger: 'chang'}],
        shr: [{required: true, message: '请选择内容', trigger: 'chang'}],
        jyy: [{required: true, message: '请选择内容', trigger: 'chang'}],
        fhy: [{required: true, message: '请选择内容', trigger: 'chang'}],
      }
    }
  },
  created() {
    this.getUserList()
    this.info.table1Json = []
  },
  methods: {
    setM4() {
      this.tableInfo.samplingAfterValue3 = this.tableInfo.samplingAfterValue2
      if (this.tableInfo.samplingAfterValue3 != null && this.tableInfo.samplingAfterValue3 != '' &&
        this.tableInfo.samplingBeforeValue3 != null && this.tableInfo.samplingBeforeValue3 != '') {
        let m4 = Number(this.tableInfo.samplingAfterValue3)
        let m2 = Number(this.tableInfo.samplingBeforeValue3)

        this.tableInfo.value1 =parseFloat((m4 - m2)).toFixed(2)

        if (this.tableInfo.value2 != null && this.tableInfo.value2 != ''
          && this.tableInfo.value3 != null && this.tableInfo.value3 != '') {
          let V = Number(this.tableInfo.value2)
          let t = Number(this.tableInfo.value3)
          this.tableInfo.value4 = parseFloat((m4 - m2) * 1000 / (V * t)).toFixed(2)
        }
      }
    },
    jisuanjg() {
      if (this.tableInfo.samplingAfterValue3 != null && this.tableInfo.samplingAfterValue3 != '' &&
        this.tableInfo.samplingBeforeValue3 != null && this.tableInfo.samplingBeforeValue3 != '') {
        let m4 = Number(this.tableInfo.samplingAfterValue3)
        let m2 = Number(this.tableInfo.samplingBeforeValue3)

        this.tableInfo.value1 = parseFloat((m4 - m2)).toFixed(2)

        if (this.tableInfo.value2 != null && this.tableInfo.value2 != ''
          && this.tableInfo.value3 != null && this.tableInfo.value3 != '') {
          let V = Number(this.tableInfo.value2)
          let t = Number(this.tableInfo.value3)
          this.tableInfo.value4 = parseFloat((m4 - m2) * 1000 / (V * t)).toFixed(2)
        }
      }
    },
    // 新增或编辑表1
    addTable1(index, row) {
      this.tableIndex = index
      this.tableInfo = {
        sampleNumber1: null,
        samplingBeforeValue1: '>2h',
        samplingBeforeValue2: null,
        samplingBeforeValue3: null,
        samplingAfterValue1: '>2h',
        samplingAfterValue2: null,
        samplingAfterValue3: null,
        value1: null,
        value2: null,
        value3: null,
        value4: null
      }
      if (row != null) {
        this.tableInfo = row
      }
      this.centerDialogVisible = true
    },
    // 保存表1
    addOrEditTable1() {
      if (this.tableIndex == null) {
        this.info.table1Json.push(this.tableInfo)
      } else {
        this.info.table1Json.splice(this.tableIndex, 1, this.tableInfo)
      }
      this.centerDialogVisible = false
    },
      // 删除表1
      delTable1(index, row) {
        this.info.table1Json.splice(index, 1)
      },
      // 获取所有用户
      getUserList() {
        getUserList().then(response => {
          this.userList = response.data
        })
      },
      getInfo(id, table1Id) {
        getInfo(id, table1Id).then(response => {
          if (response.data.table1Json != null && response.data.table1Json != '') {
            response.data.table1Json = JSON.parse(response.data.table1Json)
          } else {
            response.data.table1Json = []
          }
          if (response.data.jyy != null && response.data.jyy != '') {
            response.data.jyy = JSON.parse(response.data.jyy)
            var ids = []
            for (let i = 0; i < response.data.jyy.length; i++) {
              ids.push(response.data.jyy[i].id)
            }
            response.data.jyy = ids
          } else {
            response.data.jyy = []
          }
          if (response.data.fhy != null && response.data.fhy != '') {
            response.data.fhy = JSON.parse(response.data.fhy)
            var ids = []
            for (let i = 0; i < response.data.fhy.length; i++) {
              ids.push(response.data.fhy[i].id)
            }
            response.data.fhy = ids
          } else {
            response.data.fhy = []
          }
          this.info = response.data
        })
      },
      addOrEdit: function() {
        this.$refs['info2'].validate((valid) => {
          if (valid) {
            addOrEdit(this.info.id, this.info.tableOneId, this.info.jcrq, this.info.jcrqend, this.info.syrq, 4, JSON.stringify(this.info.table1Json), JSON.stringify(this.info.jyy), JSON.stringify(this.info.fhy),
              this.info.ypbh, this.info.jcyj, this.info.jcxm, this.info.ypxz, this.info.jcbc, this.info.bzrybh, this.info.bzrybzz, this.info.bzrydzrq, this.info.bzryyxrq,
              this.info.zlyj1, this.info.zlyl2, this.info.zlyl3, this.info.bzqx, this.info.ypcl, this.info.cytj,
              this.info.jctj1, this.info.jctj2, this.info.jctj3, this.info.jctj4, this.info.jctj5, this.info.jctj6, this.info.jctj7, this.info.jctj8,
              this.info.fcgs1, this.info.fcgs2, this.info.fcgs3, this.info.fcgs4, this.info.bfs1, this.info.bfs2, this.info.bfs3, this.info.bfs4).then(response => {
              if (this.info.id == null) {
                // 新增
                this.$notify({
                  title: '成功',
                  message: '创建成功',
                  type: 'success',
                  duration: 2000
                })
              } else {
                // 编辑
                this.$notify({
                  title: '成功',
                  message: '更新成功',
                  type: 'success',
                  duration: 2000
                })
              }
              this.$parent.$parent.getData()
              this.$parent.$parent.centerDialogVisible = false
            })
          }
        })
      },
      // 初始化
      init(id, table1Id) {
        this.getInfo(id, table1Id)
      }
    }
  }
</script>

